package com.youdelu.stock.dao.actions; 

import com.alibaba.fastjson.JSON;
import com.youdelu.stock.common.util.HttpsUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


public class BaseAction { 
    public static final String DOMAIN = "https://xueqiu.com";
    public static final String DOMAIN_XUEQIU = "https://stock.xueqiu.com";
 

    public static String DOMAIN_DFCF = "https://np-anotice-stock.eastmoney.com/api";
    public static String DOMAIN_DFCF_PUSH2E = "http://push2ex.eastmoney.com";

    public static String DOMAIN_JCZXAPI = "http://webapi.cninfo.com.cn/api";
    public static String DOMAIN_JCZX = "http://www.cninfo.com.cn";

    private final static String CONTENT_TYPE = "application/json";
    private final static String CONTENT_TYPE_2 = "application/x-www-form-urlencoded";
    private final static String ENCODING = "utf-8";
 

    /**
     * 构造方法 
     */
    public BaseAction() {
       
    }


    /**
     * post
     * @param domain
     * @param url
     * @param cla
     * @param obj
     * @param <T>
     * @return
     */
    public <T>T post(String domain,String url,Class<T> cla,Object obj) {
        String urls = new StringBuffer(domain).append("/").append(url).toString() ;
        String json = HttpsUtils.doPost(urls,obj);
        return JSON.parseObject(json,cla);
    }


    /**
     *  get
     * @param domain
     * @param url
     * @param cla
     * @param obj
     * @param <T>
     * @return
     */
    public <T>T get(String domain,String url,Class<T> cla,Object obj){
        String urls = new StringBuffer(domain).append("/").append(url).toString() ;
        String json = HttpsUtils.doGetString(urls,classToMap(obj),null);
        return JSON.parseObject(json,cla);
    }


    /**
     * 类转map
     * @param obj
     * @return
     */
    public static Map<String,Object> classToMap(Object obj){
        Map<String,Object> map = new HashMap<>();
        if(obj==null){
            return map;
        }
        Class<?> cla = obj.getClass();
        Field[] fa =  cla.getDeclaredFields();;
        int i = 0 ;
        for(Field f:fa){
            f.setAccessible(true);
            try {
                Object val = f.get(obj);
                if(val!=null) {
                    map.put(f.getName(),val);
                }
            }catch (Exception e){
            }
        }
        return map;
    }


    /**
     * 获取完整URL属性值
     * @param url
     * @param obj
     * @return
     */
    protected static String getURL(String url, Object obj) {
        StringBuilder urlBuilder = new StringBuilder(url);
        if(obj!=null){
            urlBuilder.append("?");
            Class<?> cla = obj.getClass();
            Field[] fa =  cla.getDeclaredFields();;
            int i = 0 ;
            for(Field f:fa){
                f.setAccessible(true);

                try {
                    Object val = f.get(obj);
                    if(val!=null) {
                        String name = f.getName();
                        urlBuilder.append(name + "=" + val);
                        if (i < fa.length - 1) {
                            urlBuilder.append("&");
                        }
                    }
                }catch (Exception e){
                }
                i++;
            }
        }
        return urlBuilder.toString();
    }

}
